% note here we only plot the case where the agent does not adjust
clear all;
cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','adjust_noad_ini',...
    'nb','ntheta','nz','gridb','gridtheta','gridz','nb_pdf','ntheta_pdf','gridb_pdf','gridtheta_pdf');
cd 'code_figure_table\OA\Figure OA5';

farmarketindex=2;
closemarketindex=1;

talent=2.1;
[~,index]=min(abs(gridz-talent));

figure('color',[1,1,1],'Position',[100,100,400,320]);

%% far market
occupation_choice=squeeze(adjust_noad_ini(farmarketindex,index,:,:))';
worker_entre_boundary(1:ntheta)=0.0;
entre_credit_lower_boundary(1:ntheta)=0.0;
entre_credit_upper_boundary(1:ntheta)=0.0;

for i_theta=1:ntheta
    index2=find(occupation_choice(i_theta,:)==2);
    index3=find(occupation_choice(i_theta,:)==3);
    if (isempty(index3) && isempty(index2))
        worker_entre_boundary(i_theta)=nb;
        entre_credit_lower_boundary(i_theta)=nb;
        entre_credit_upper_boundary(i_theta)=nb;
    elseif (isempty(index3) && ~isempty(index2))
        worker_entre_boundary(i_theta)=min(index2);
        entre_credit_lower_boundary(i_theta)=nb;
        entre_credit_upper_boundary(i_theta)=nb;
    elseif (~isempty(index3) && isempty(index2))
        worker_entre_boundary(i_theta)=min(index3);
        entre_credit_lower_boundary(i_theta)=min(index3);
        entre_credit_upper_boundary(i_theta)=max(index3);
    else
        worker_entre_boundary(i_theta)=min(min(index2),min(index3));
        entre_credit_lower_boundary(i_theta)=min(index3);
        entre_credit_upper_boundary(i_theta)=max(index3);
    end
end
griddeposit=1-gridtheta;

start_from=ntheta;
% remodify the entre_credit_lower_boundary and entre_credit_upper_boundary
x1=gridb(worker_entre_boundary);
y1=griddeposit;
x2=flip(gridb(entre_credit_lower_boundary(1:start_from)));
y2=flip(griddeposit(1:start_from));
x3=gridb(entre_credit_upper_boundary(1:start_from));
y3=griddeposit(1:start_from);

plot(x1,y1,'-b','linewidth',2)
hold on
plot(x2,y2,'--b','linewidth',2)
hold on
plot(x3,y3,'--b','linewidth',2)

xlim([0,20]);
ylim([0,1]);


%% near market
occupation_choice=squeeze(adjust_noad_ini(closemarketindex,index,:,:))';
worker_entre_boundary(1:ntheta)=0.0;
entre_credit_lower_boundary(1:ntheta)=0.0;
entre_credit_upper_boundary(1:ntheta)=0.0;

for i_theta=1:ntheta
    index2=find(occupation_choice(i_theta,:)==2);
    index3=find(occupation_choice(i_theta,:)==3);
    if (isempty(index3) && isempty(index2))
        worker_entre_boundary(i_theta)=nb;
        entre_credit_lower_boundary(i_theta)=nb;
        entre_credit_upper_boundary(i_theta)=nb;
    elseif (isempty(index3) && ~isempty(index2))
        worker_entre_boundary(i_theta)=min(index2);
        entre_credit_lower_boundary(i_theta)=nb;
        entre_credit_upper_boundary(i_theta)=nb;
    elseif (~isempty(index3) && isempty(index2))
        worker_entre_boundary(i_theta)=min(index3);
        entre_credit_lower_boundary(i_theta)=min(index3);
        entre_credit_upper_boundary(i_theta)=max(index3);
    else
        worker_entre_boundary(i_theta)=min(min(index2),min(index3));
        entre_credit_lower_boundary(i_theta)=min(index3);
        entre_credit_upper_boundary(i_theta)=max(index3);
    end
end
griddeposit=1-gridtheta;

start_from=ntheta;
% remodify the entre_credit_lower_boundary and entre_credit_upper_boundary
x4=gridb(worker_entre_boundary);
y4=griddeposit;
x5=flip(gridb(entre_credit_lower_boundary(1:start_from)));
y5=flip(griddeposit(1:start_from));
x6=gridb(entre_credit_upper_boundary(1:start_from));
y6=griddeposit(1:start_from);

plot(x4,y4,':r','linewidth',2)
hold on
plot(x5,y5,':r','linewidth',2)
hold on
plot(x6,y6,':r','linewidth',2)

xlim([0,20]);
ylim([0,1]);

save('wealth_deposit_ratio.mat','x1','y1','x2','y2','x3','y3','x4','y4','x5','y5','x6','y6')